package com.myapp.mobilesafe.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.myapp.mobilesafe.db.dao.CommonNumberDao;

import java.util.ArrayList;

public class CommonNumberActivity extends AppCompatActivity {
    private ExpandableListView elvList;
    private CommonNumberAdapter mAdapter;
    private ArrayList<CommonNumberDao.GroupInfo> mCommonNumerGroups;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_number);
        elvList = (ExpandableListView) findViewById(R.id.elv_list);
        mCommonNumerGroups = CommonNumberDao.getCommonNumberGroups();
        mAdapter = new CommonNumberAdapter();
        elvList.setAdapter(mAdapter);

        elvList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                System.out.println("第" + groupPosition + "组-第" + childPosition
                        + "项被点击");
                CommonNumberDao.ChildInfo child = mAdapter.getChild(groupPosition,
                        childPosition);

                // 跳转到打电话页面
                // 需要权限:CALL_PHONE
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + child.number));
                startActivity(intent);
                return false;
            }
        });
    }
    class CommonNumberAdapter extends BaseExpandableListAdapter {

        // 返回组的数量
        @Override
        public int getGroupCount() {
            // return 8;
            return mCommonNumerGroups.size();
        }

        // 返回每个组孩子的数量
        @Override
        public int getChildrenCount(int groupPosition) {
            // return groupPosition + 1;
            return mCommonNumerGroups.get(groupPosition).children.size();
        }

        // 返回组的对象getItem()
        @Override
        public CommonNumberDao.GroupInfo getGroup(int groupPosition) {
            return mCommonNumerGroups.get(groupPosition);
        }

        // 返回孩子的对象getItem()
        @Override
        public CommonNumberDao.ChildInfo getChild(int groupPosition, int childPosition) {
            return mCommonNumerGroups.get(groupPosition).children
                    .get(childPosition);
        }

        // 返回组id, getItemId()
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        // 返回孩子id, getItemId()
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        // 组的布局 getView
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            TextView view = new TextView(getApplicationContext());
            view.setTextColor(Color.BLUE);
            view.setTextSize(20);
            // view.setText("       第" + groupPosition + "组");
            CommonNumberDao.GroupInfo group = getGroup(groupPosition);
            view.setText("      " + group.name);
            return view;
        }

        // 孩子的布局 getView
        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            TextView view = new TextView(getApplicationContext());
            view.setTextColor(Color.WHITE);
            view.setTextSize(18);
            // view.setText("第" + groupPosition + "组-第" + childPosition + "项");
            CommonNumberDao.ChildInfo child = getChild(groupPosition, childPosition);
            view.setText(child.name + "\n" + child.number);
            return view;
        }

        // 表示孩子是否可以点击
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }
}
